package Module;

import Communication.S2ModulePacket;
import java.io.ObjectInputStream;
import Const.ModuleConst;
/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class ModuleReceiver extends Thread {


    private ModuleClient moduleClient;
    private S2ModulePacket inpacket;

    /**
     * Get the value of inpacket
     *
     * @return the value of inpacket
     */
    public S2ModulePacket getInpacket() {
        return inpacket;
    }

    /**
     * Set the value of inpacket
     *
     * @param inpacket new value of inpacket
     */
    public void setInpacket(S2ModulePacket inpacket) {
        this.inpacket = inpacket;
    }

    /**
     * Get the value of moduleClient
     *
     * @return the value of moduleClient
     */
    public ModuleClient getModuleClient() {
        return moduleClient;
    }

    /**
     * Set the value of moduleClient
     *
     * @param moduleClient new value of moduleClient
     */
    public void setModuleClient(ModuleClient moduleClient) {
        this.moduleClient = moduleClient;
    }

    /**
    * Default constructor.
    */
    public ModuleReceiver(ModuleClient moduleClient){
        this.moduleClient = moduleClient;

    }

    @Override
    public void run()
    {
        try {
                while(moduleClient.isConnected())
                {
                        ObjectInputStream ois = new ObjectInputStream(moduleClient.getSocket().getInputStream());
                        Object readObject = ois.readObject();

                    if ( readObject instanceof S2ModulePacket ){
                        synchronized(this.moduleClient.getTaskList()){
                            inpacket = (S2ModulePacket)readObject;

                            this.moduleClient.setTaskList(inpacket.getTaskList());
                            this.moduleClient.getLiftRef().setCurrentFloor(inpacket.getCurrentFloor());
                            this.moduleClient.setLastDoneID(inpacket.getLastDoneTaskID());

                            //System.out.println("Dostałem pakiet typu S2ModulePacket");
                                                        
                                if(moduleClient.getTaskDone().size() == 0 && moduleClient.getTaskList().size()>0)
                                {
                                    moduleClient.getTaskDone().add(moduleClient.getTaskList().getFirst());
                                }
                                for(int j = 0; j < moduleClient.getTaskList().size(); j++)
                                {
                                    if(moduleClient.getTaskDone().getLast().getTaskID() < moduleClient.getTaskList().get(j).getTaskID()){
                                        //System.out.println("Lista w pakiecie S2ModulePacket nie byla pusta i spelnia warunek dodania.");
                                        moduleClient.getTaskDone().add(moduleClient.getTaskList().get(j));
                                    }
                                }
                       }
                        Thread.sleep(ModuleConst.TimeDelayShort);
                    }
                }
        }
        catch(Exception ex)
                {
                    System.out.println(":TTXXS");
                    System.out.println(ex.getClass() + "\t" + ex.getMessage());
                }

    }


}//end class
